<template>
  <el-dialog v-loading="loading" title="App授权" :close-on-click-modal="false" :visible.sync="visible">
    <el-tree ref="menuListTree" :data="menuList" show-checkbox node-key="id" :props="defaultProps">
    </el-tree>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    treeDataTranslate
  } from '@/utils'
  export default {
    data() {
      return {
        visible: false,
        loading: false,
        defaultCheckedKeys: [],
        dataForm: {
          id: 0,
          roleName: '',
        },
        menuList: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
      }
    },
    methods: {
      init(id) {
        Object.assign(this.$data, this.$options.data())
        this.dataForm.id = id || 0
        this.loading = true;
        this.$http({
          url: this.$http.adornUrl('/sys/menu/applist'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          this.menuList = treeDataTranslate(data, 'id')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs.menuListTree.setCheckedKeys([])
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/role/appInfo/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.roleName = data.role.roleName
                var idx = data.role.menuIdList.indexOf(this.tempKey)
                if (idx !== -1) {
                  data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx)
                }
                this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList)
              }
              this.loading = false;
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$http({
          url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'appSave' : 'appupdate'}`),
          method: 'post',
          data: this.$http.adornData({
            'roleId': this.dataForm.id || undefined,
            'roleName': this.dataForm.roleName,
            'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree
              .getHalfCheckedKeys())
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: this.$t('Common.OperationSuccessful'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
